$(function() {
	$(".context").height($(window).height() - 95);
	$("#grid").simplegrid({
		onClick: function(element, user) {
			$("#role-ul").tree({
				url: "system/Role/user?userid=" + user.id
			});
			$("#group-ul").tree({
				url : "system/Group/getRoleTree?userid=" +user.id
			});
			$("#xingzqh-ul").tree({
				url : "system/BmbXingzqh/xingzqh?userid=" +user.id
			});
		}
	});
});

/**
 * 添加
 */
function btn_add() {
	$("#userinfo-button").unbind();
	$("#userinfo-button").click(exec_add);
	$("#userinfo-form")[0].reset();
	$("#userinfo-form [name='userInfo.username']").attr("disabled", false);
	$("#userinfo-div").window({title: '添加', iconCls:'icon-add', closed: false});
}

function exec_add() {
	var data = $("#userinfo-form").serializeArray();
	$.ajax({
		url: 'system/User/add',
		type: 'post',
		dataType: 'json',
		data: data,
		success: function(data) {
			if(data.result > 0) {
				alert("添加成功");
				$("#grid").simplegrid("refresh");
			} else {
				this.error();
			}
		},
		error: function() {
			alert("添加失败");
		}
	});
}

/** 编辑 */
function btn_edit() {
	var user = $("#grid").simplegrid("getSelected");
	if (user.length == 0) {
		alert("请选择一条记录.");
		return;
	}
	user = user[0];
	$("#userinfo-button").unbind();
	$("#userinfo-button").click(exec_edit);
	$("#userinfo-form [name='userInfo.username']").attr("disabled", true);
	$("#userinfo-form").form("load", {
		'userInfo.id': user.id,
		'userInfo.username': user.username,
		'userInfo.password': user.password,
		'userInfo.name': user.name,
		'userInfo.sex': user.sex,
		'userInfo.state': user.state
	});
	$("#userinfo-password-hide").val("***************");
	$("#userinfo-password-show").val("***************");
	$("#userinfo-div").window({title: '修改', iconCls:'icon-edit', closed: false});
}

function exec_edit() {
	var data = $("#userinfo-form").serializeArray();
	$.ajax({
		url: 'system/User/edit',
		type: 'post',
		dataType: 'json',
		data: data,
		success: function(data) {
			if(data.result > 0) {
				alert("编辑成功");
				$("#grid").simplegrid("refresh");
			} else {
				this.error();
			}
		},
		error: function() {
			alert("编辑失败");
		}
	});
}

/** 删除 */
function btn_delete() {
	var user = $("#grid").simplegrid("getSelected");
	if (user.length == 0) {
		alert("没有选择记录.");
		return;
	}
	user = user[0];
	if (confirm("确认删除[" + user.username + "]吗?")) {
		$.ajax({
			url: 'system/User/del',
			type: 'post',
			data: {id: user.id},
			success: function() {
				alert("删除成功");
				$("#grid").simplegrid("refresh");
			},
			error: function() {
				alert("删除失败");
			}
		});
	}
}

function exec_role() {
	var user = $("#grid").simplegrid("getSelected");
	user = user[0];
	var role = $("#role-ul").tree("getChecked");
	var param = new Array();
	param.push({name: 'exec', value: 'set'});
	param.push({name: 'userid', value: user.id});
	for ( var i = 0; i < role.length; i++) {
		if ($('#role-ul').tree('isLeaf', role[i].target)) {
			param.push({name: 'roleid', value: role[i].id});
		}
	}
	$.ajax({
		url: 'system/Role/user',
		type: 'post',
		dataType: 'json',
		data: param,
		success: function(data) {
			if (data.result > 0) {
				alert("设置成功");
			} else {
				this.error();
			}
		},
		error: function(e) {
			alert("设置失败:" + e);
		}
	});
}


function exec_group() {
	var user = $("#grid").simplegrid("getSelected");
	user = user[0];
	var group = $("#group-ul").tree("getChecked");
	var param = new Array();
	param.push({name: 'exec', value: 'set'});
	param.push({name: 'userid', value: user.id});
	for ( var i = 0; i < group.length; i++) {
		if ($('#group-ul').tree('isLeaf', group[i].target)) {
			param.push({name: 'groupid', value: group[i].id});
		}
	}
	$.ajax({
		url: 'system/Group/group',
		type: 'post',
		dataType: 'json',
		data: param,
		success: function(data) {
			if (data.result > 0) {
				alert("设置成功");
			} else {
				this.error();
			}
		},
		error: function(e) {
			alert("设置失败:" + e);
		}
	});
}


function exec_xingzqh() {
	var user = $("#grid").simplegrid("getSelected");
	user = user[0];
	var xingzqh = $("#xingzqh-ul").tree("getChecked");
	var param = new Array();
	param.push({name: 'exec', value: 'set'});
	param.push({name: 'userid', value: user.id});
	for ( var i = 0; i < xingzqh.length; i++) {
		if ($('#xingzqh-ul').tree('isLeaf', xingzqh[i].target)) {
			param.push({name: 'xingzqhbh', value: xingzqh[i].id});
		}
	}
	$.ajax({
		url: 'system/BmbXingzqh/xingzqh',
		type: 'post',
		dataType: 'json',
		data: param,
		success: function(data) {
			if (data.result > 0) {
				alert("设置成功");
			} else {
				this.error();
			}
		},
		error: function(e) {
			alert("设置失败:" + e);
		}
	});
}

var chars = ['0','1','2','3','4','5','6','7','8','9', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
function random_pwd() {
	var pwd = "";
	for ( var int = 0; int < 6; int++) {
		var id = Math.ceil(Math.random()*35);
		pwd += chars[id];
	}
	$("#userinfo-password-show").val(pwd);
	$("#userinfo-password-hide").val(pwd);
}
